<?php
class M_levels extends CI_Model {
	
	private $__table = 'levels';
	
	/**
	 * Get All Levels for Approval based on hierarchy
	 * @param int $level_id
	 */
	public function get_all_levels_array($level_id = false)
	{
		if($level_id === false){
			$query = $this->db->select(array('level_id','level_desc','level_code'))->get($this->__table);
			
		}else{
			$lvl = $this->db->select('level_hierarchy as rank')->where('level_id',$level_id)->get($this->__table);
			$lvl = $lvl->num_rows() > 0 ? $lvl->row() : FALSE;

			if($lvl == false){
				$this->get_all_levels_array();
			}else{
				$query = $this->db->select(array('level_id','level_desc','level_code'))->where('level_hierarchy >= ',$lvl->rank)->get($this->__table);	
			}
		}

		if($query->num_rows() > 0)
		{
			$result[''] ='-- Choose A Level --';
			foreach($query->result() as $q)
			{
				$result[$q->level_id] = $q->level_code.' | '.$q->level_desc;
			}
			return $result;
		}else{
			return FALSE;
		}
	}
	
	public function check_for_level_cat($id,$type)
	{
	
		$type = array_map('strtolower',$type);
		
		$q = $this->db->select('level_category')
					  ->where('level_id',$id)
					  ->where_in('level_category',$type)
					  ->get('levels');
					  
		if($q->num_rows() >=1)
		{
			$q = strtolower($q->row()->level_category);
			return in_array($q,$type) ? TRUE : FALSE;
		}else{
			return FALSE;
		}
	}
}
?>